<?php
/*
 * Created on Feb 10, 2008
 *
 * This is an open source software, please obey the GPL
 * The Referee Service is offered by Cacomand Inc. 
 * View Http://www.cacomand.com for support or more information
 * You can also view distrema.googlecode.com to join the community.
 */
 //定义旅行社名称
 $agency_name		="杭州客车旅游公司";
 $agency_epsn		="bj_of_0001";
 $agency_user_ID	=3;
 $agency_password	="1234";
 $extra_password	="extra_password";
 include("lib_dps.php");
 class agency_staff extends dps_user
 {
 	function trip_payed($traveler,$trip)
 	{
 		$rec_array=$this->smart_get(false,19);
 		foreach($rec_array as $rec)
 		{
 			if($rec["caption"]==$trip->trip_full_name AND $rec["memo"] == $traveler->epsn.".".$traveler->user_ID)//出团计划中存在这个未付款用户
 			{
 				$this->add_record(20,$trip->trip_full_name,$rec["memo"]);
 				$this->del_record(19,$trip->trip_full_name,$rec["memo"]);
 				give_a_confirm_form("agc_finish.php","确认付款");
 				return true;
 			}
 		}
 		return false;
 	}
 	
 	function show_all_unpayed_trips()
 	{
 		global $agc_pay_processor,$agency_name;
 		$rec_array=$this->smart_get(false,19);
 		echo('<form method="POST" action="'.$agc_pay_processor.'">');
 		echo('<td><input type="hidden" name="username" value="'.$_POST["username"].'"></td>');
 		echo('<td><input type="hidden" name="password" value="'.$_POST["password"].'"></td>');
 		echo('<table border="0" align="left" cellspacing="2" cellpadding="2" width="100%" bgcolor="#ff8718">');
		echo('<caption>所有未付款的订单 </caption>');
		echo('<tr>');
		echo('<td>线路名称</td>');
		echo('<td>出发时间</td>');
		echo('<td>用户姓名</td>');
		echo('<td>付款</td>');
		echo('</tr>');
 		foreach($rec_array as $rec)
 		{
 			list($agency_name,$route_name,$set_off_time)=explode(".",$rec["caption"]);
 			list($traveler_epsn,$traveler_user_ID)=explode(".",$rec["memo"]);
 			$tmp=$this->smart_get(false,23,"姓名");
 			if($tmp[0]["memo"]==$agency_name)
 			{
	 			$traveler=new traveler($traveler_epsn,$traveler_user_ID);
	 			$realname=$traveler->smart_get(true,23);
	 			echo('<tr>');
				echo('<td>'.$route_name.'</td>');
				echo('<td>'.$set_off_time.'</td>');
				echo('<td>'.$realname[0]["memo"].'</td>');
				echo('<td><input type="radio" name="trip_to_pay" value="'.$rec["caption"].".".$rec["memo"].'"></td>');
				echo('</tr>');
 			}
 		}
 		echo("</table><br />");
 		echo('<input type="submit" name="submit" value="确认其付款"><br />');
		echo('<a href=../online_payment/view_my_income.php target="_blank">在线收款</a>');
 		echo('</form>');
 	}
	
	function clear_up()
 	{
 		global $agency_password,$dps_pwuser,$dps_pwpswd;
		//先例行上传一次
		$dps=new dps_action($dps_pwuser,$dps_pwpswd);
		@$dps->great_upload();
		@$dps->preload();
		//先清除掉过期而未付款的订单
		if($rec_array=$this->smart_get(false,19))
		{
			foreach($rec_array as $rec)
			{
				list($agency_name,$route_name,$set_off_time)=explode(".",$rec["caption"]);
				list($oy,$om,$od)=explode("-",$set_off_time);
				$ny=date("Y");
				$nm=date("m");
				$nd=date("d");
				if($ny>$oy OR ($ny>=$oy AND $nm>$om) OR ($ny>=$oy AND $nm>=$om AND $nd>$od))
					$this->del_record(19,$rec["caption"],$rec["memo"]);
			}
		}
		
		//上传更新
		$dps->user_upload($this->epsn,$this->user_ID,$agency_password);
 	}
 }
 
 class traveler extends dps_user
 {
 	public $realname,$gender,$mobile;
 	function traveler($epsn,$user_ID)
 	{
 		if(func_num_args()==3)
		{
			$trip_full_name=func_get_arg(2);
		}	
		else
		{
			$trip_full_name="";
		}
		parent::dps_user($epsn,$user_ID);
 		$tmp=$this->smart_get(true,24);
 		$this->gender=$tmp[0]["memo"];
 		
 		$tmp=$this->smart_get(true,14,"常用");
 		$this->mobile=$tmp[0]["memo"];
 		
 		$tmp=$this->smart_get(true,23,"姓名");
 		$this->realname=$tmp[0]["memo"];
		if($trip_full_name!="")
		{
			$tmp=$this->smart_get(true,21,$trip_full_name);
			$this->address=$tmp[0]["memo"];
		}
 	}
 }
 
 class travel_agency extends service
 {
 	public $service_name="旅行社";
 	function show_default_links()
	{
		global $pay_processor,$agency_epsn,$agency_user_ID;
		$agency=new dps_user($agency_epsn,$agency_user_ID);
		if(islogedin())
		{
			echo("<a href=pay_for_trips.php>为您的行程付款</a><br />");
			echo("<a href=view_trips.php>管理您的行程</a><br />");
			echo('<a href=../user_login.php target="blank">回到登录页面</a><br />');
			echo('<a href=../dps_upload.php target="blank">立刻同步您的信息(高级)</a><br />');
		}
		echo("<a href=view_routes.php>浏览其他的线路</a><br />");
		echo("<a href=index.php>回到主页</a><br />");
	}
 	
 	function focus_route($route)
 	{
 		if(setcookie("last_viewed_route",$route->route_full_name,time()+(3600*24)))
 		{
 			echo("设置了焦点路线<br />");
 			return true;
 		}
 		else
 			return false;
 	
 	}
 	
 	function get_focused_route()
 	{
 		if(isset($_COOKIE["last_viewed_route"]))
 		{
	 		$route_to_return=new route($_COOKIE["last_viewed_route"]);
	 		return $route_to_return;
 		}
 		else
 		{
 			return false;
 		}
 	}
 	
 	function focus_trip($trip)
 	{
 		if(setcookie("focused_trip",$trip->trip_full_name,time()+(3600*24)))
 		{
 			echo("设置了焦点出团计划".$trip->trip_full_name."<br />");
 			return true;
 		}
 		else
 			return false;
 	
 	}
 	
 	function get_focused_trip()
 	{
 		if(isset($_COOKIE["focused_trip"]))
 		{
	 		$trip_to_return=new trip($_COOKIE["focused_trip"]);
	 		echo("读取了焦点出团计划");
	 		return $trip_to_return;
 		}
 		else
 		{
 			return false;
 		}
 	}
 	
	function cancel_trip($trip,$traveler)
	{
		global $ccr_public_username;
		global $ccr_public_key;
		global $agency_epsn,$agency_user_ID;
		global $developer;
		$agency=new user($agency_epsn,$agency_user_ID);
		delete_data($agency,19,$trip->trip_full_name,$traveler->epsn.".".$traveler->user_ID);
		$traveler->del_record(21,$trip->trip_full_name);
	}
	
 	function give_an_apply_form()
	 {
	 	global $user_manage_processor;
		if(islogedin())
	 	{
	 		$apply_user=get_current_user_obj();
	 		$tmp_array=$apply_user->smart_get(true,23,"姓名");
	 		$realname=$tmp_array[0]["memo"];
	 		
	 		$tmp_array=$apply_user->smart_get(true,24);
	 		$gender=$tmp_array[0]["memo"];
	 		
	 		$tmp_array=$apply_user->smart_get(true,14,"常用");
	 		$mobile=$tmp_array[0]["memo"];
	 		
	 		$tmp_array=$apply_user->smart_get(true,21);
	 		$pick_up_address=$tmp_array[0]["memo"];
	 		
	 		global $apply_confirm_processor;
	 		echo('<form method="POST" action="'.$apply_confirm_processor.'">');
	 		echo('<table width="200" border="0" align="center">');
			echo('<caption>您的信息(我们会尽量从您以前使用过的DistRema服务中获取您的信息,请您确认)</caption>');
			
			echo('<tr>');
			echo('<td bgcolor="#D2EFFF">真实姓名:</td>');
			echo('<td bgcolor="#D2EFFF"><input type=text name=realname id=kw size=36 maxlength=100 value='.$realname.'></td>');
			echo('</tr>');
			
			echo('<tr>');
			echo('<td bgcolor="#E3E3E3">性别:</td>');
			if($gender=="男")
			{
				echo('<td bgcolor="#E3E3E3"><input name="gender" id=kw type="radio" value="男" checked="checked" />男<input name="gender" id=kw type="radio" value="女" />女</td>');
			}
			elseif($gender=="女")
			{
				echo('<td bgcolor="#E3E3E3"><input name="gender" id=kw type="radio" value="男" />男<input name="gender" id=kw type="radio" value="女" checked="checked" />女</td>');
			}
			else
			{
				echo('<td bgcolor="#E3E3E3"><input name="gender" id=kw type="radio" value="男" />男<input name="gender" id=kw type="radio" value="女" />女</td>');
			}
			echo('</tr>');
			
			echo('<tr>');
			echo('<td bgcolor="#D2EFFF">常用手机号码:</td>');
			echo('<td bgcolor="#D2EFFF"><input type=text name=mobile id=kw size=36 maxlength=100 value='.$mobile.'></td>');
			echo('</tr>');
			
			echo('<tr>');
			echo('<td bgcolor="#E3E3E3">出发日期:</td>');
			echo('<td bgcolor="#E3E3E3"><input type=text name=set_off_year id=kw size=4 maxlength=100 value="'.date("Y").'">年<input type=text name=set_off_month id=kw size=2 maxlength=100 value="">月<input type=text name=set_off_day id=kw size=2 maxlength=100 value="">日</td>');
			echo('</tr>');
			
			echo('<tr>');
			echo('<td bgcolor="#D2EFFF">接客地址:</td>');
			echo('<td bgcolor="#D2EFFF"><input type=text name=pick_up_address id=kw size=36 maxlength=100 value='.$pick_up_address.'></td>');
			echo('</tr>');
			
			echo('</table>');
			echo('<br /><input type="submit" name="submit" value="现在报名"  align="center"><br />');
 			echo('</form>');
	 	}
	 	else
	 	{
	 		echo('您尚未登录DistRema平台<br />');
	 		echo('请点击<a href=../user_login.php target="blank">这里</a>登录DistRema平台');
	 		echo('或点击<a href=contact.html>这里</a>进入电话报名');
	 	}
	 }
	 
	 function list_unpayed_trips()
	 {
	 	global $agency_epsn,$agency_user_ID;
		global $color1,$color2;
	 	global $trip_detail_processor;
	 	$agency=new dps_user($agency_epsn,$agency_user_ID);
		$user=get_current_user_obj();
		$color_switch=false;	
		echo('<table width="200" border="0" align="center">');
		echo('<caption>您尚未付款的路线</caption>');
		echo('<tr>');
		echo('<th scope="col">旅行社</th>');
		echo('<th scope="col">路线名称</th>');
		echo('<th scope="col">出发时间</th>');
		echo('<th scope="col">选择</th>');
		echo('</tr>');
		$m=0;
		if($unpayed_routes=$agency->smart_get(false,19))
		{
			foreach($unpayed_routes as $unpayed_route)
			{
				if($unpayed_route["memo"]==$user->epsn.'.'.$user->user_ID)
				{
			 		$my_unpayed_routes[]=$unpayed_route["caption"];
			 		list($agency_name,$route_name,$set_off_time)=explode(".",$unpayed_route["caption"]);
					if($color_switch)
					{
						$color=$color1;
						$color_switch=false;
					}
					else
					{
						$color=$color2;
						$color_switch=true;
					}
					echo('<tr>');
					echo('<td bgcolor='.$color.'>'.$agency_name.'</td>');
					echo('<td bgcolor='.$color.'>'.$route_name.'</td>');
					echo('<td bgcolor='.$color.'>'.$set_off_time.'</td>');
					echo('<td bgcolor='.$color.'><input type="radio" name="trip_full_name" value="'.$agency_name.".".$route_name.".".$set_off_time.'"></td>');
					echo('</tr>');
					$m++;
				}
			}
		}
		if($m==0)
			echo('<tr><td bgcolor='.$color1.'>您尚无未付款的行程</td></tr>');
		echo('</table><br />');
	 }
	 
	 function list_payed_trips()
	 {
	 	global $agency_epsn,$agency_user_ID;
		global $color1,$color2;
	 	global $trip_detail_processor;
	 	$agency=new dps_user($agency_epsn,$agency_user_ID);
		$user=get_current_user_obj();
		$color_switch=false;			
		echo('<table width="200" border="0" align="center">');
		echo('<caption>您已付款并确认的路线</caption>');
		echo('<tr>');
		echo('<th scope="col">旅行社</th>');
		echo('<th scope="col">路线名称</th>');
		echo('<th scope="col">出发时间</th>');
		echo('<th scope="col">选择</th>');
		echo('</tr>');
		$m=0;
		if($payed_routes=$agency->get_var_from_ccr(20))
		{
			foreach($payed_routes as $payed_route)
			{
				if($payed_route["memo"]==$user->epsn.'.'.$user->user_ID)
				{
			 		$my_payed_routes[]=$payed_route["caption"];
			 		list($agency_name,$route_name,$set_off_time)=explode(".",$payed_route["caption"]);
					if($color_switch)
					{
						$color=$color1;
						$color_switch=false;
					}
					else
					{
						$color=$color2;
						$color_switch=true;
					}
					echo('<tr>');
					echo('<td bgcolor='.$color.'>'.$agency_name.'</td>');
					echo('<td bgcolor='.$color.'>'.$route_name.'</td>');
					echo('<td bgcolor='.$color.'>'.$set_off_time.'</td>');
					echo('<td bgcolor='.$color.'><input type="radio" name="trip_full_name" value="'.$agency_name.".".$route_name.".".$set_off_time.'"></td>');
					echo('</tr>');
					$m++;
				}
			}
		}
		if($m==0)
			echo('<tr><td bgcolor='.$color1.'>您尚无已付款的行程</td></tr>');
		echo('</table><br />');
	 }
 }
 
 class route_list
 {
 	private $target_agency;
 	
 	function list_routes()
 	{
 		global $agency_name,$agency_epsn,$agency_user_ID,$developer;
		global $color1,$color2;
		$color_switch=false;
 		$target_agency=new dps_user($agency_epsn,$agency_user_ID);
 		$this->begin_a_route_select_form();
 		$this->begin_a_route_list();
 		$route_array=$target_agency->smart_get(false,16); 		
 		foreach($route_array as $route_single)
 		{
 			$route_to_list=new route($route_single["caption"]);
 			if($color_switch)
			{
				$color=$color1;
				$color_switch=false;
			}
			else
			{
				$color=$color2;
				$color_switch=true;
			}
			$this->show_a_row_for_a_route($route_to_list,$color);
 		}
 		$this->end_a_route_list();
 		$this->end_a_route_select_form();
 	}
 	function begin_a_route_select_form()
 	{
 		global $route_detail_processor;
 		echo('<form method="POST" action="'.$route_detail_processor.'">');
 	} 	
 	
 	function begin_a_route_list()
 	{
 		echo('<table width="200" border="0" align="center">');
		echo('<caption>所有线路 </caption>');
		echo('<tr>');
		echo('<th scope="col">线路名称</th>');
		echo('<th scope="col">门市价</th>');
		echo('<th scope="col">网上预定价</th>');
		echo('</tr>');
 	}
 	
 	function show_a_row_for_a_route($route,$color)
 	{
 		echo('<tr>');
		echo('<td bgcolor='.$color.'><a href="route_detail.php?route_full_name='.urlencode($route->route_full_name).'" class="link_green_dark">'.$route->route_name.'</a></td>');
		echo('<td bgcolor='.$color.'>'.$route->route_original_price."</td>");
		echo('<td bgcolor='.$color.'>'.$route->route_special_price."</td>");
		echo('</tr>');
 	}
 	
 	function end_a_route_list()
 	{
 		echo('</table>');
 	}
 	
 	function end_a_route_select_form()
 	{
 		echo('</form>');
 	}
 }
 
 class route
 {
 	public $route_name,$route_full_name,$route_memo,$route_arrange,$route_fee,$route_original_price,$route_special_price,$agency_name;
 	public $agency;
 	function route($route_full_name)
 	{
 		global $agency_epsn,$agency_user_ID;
 		$this->agency=new dps_user($agency_epsn,$agency_user_ID);
 		list($agency_name,$this->route_name)=explode(".",$route_full_name);
 		$this->route_full_name=$route_full_name;
 		$memo_array=$this->agency->smart_get(false,16,$this->route_full_name);//获取线路介绍
 		$this->route_memo=$memo_array[0]["memo"];
 		$arg_array=$this->agency->smart_get(false,28,$this->route_full_name);//获取线路网上预定价
 		$this->route_arrange=$arg_array[0]["memo"];
 		$fee_array=$this->agency->smart_get(false,29,$this->route_full_name);//获取线路网上预定价
 		$this->route_fee=$fee_array[0]["memo"];
 		$op_array=$this->agency->smart_get(false,17,$this->route_full_name);//获取线路常规预定价
 		$this->route_original_price=$op_array[0]["memo"];
 		$sp_array=$this->agency->smart_get(false,18,$this->route_full_name);//获取线路网上预定价
 		$this->route_special_price=$sp_array[0]["memo"];
 		
 	}
 }
 
 class trip
 {
 	public $agency_name,$route_name,$set_off_time,$route_full_name,$trip_full_name,$user_number;
 	public $agency_belong,$route_belong,$users_belong;
 	function trip($trip_full_name)
 	{
 		global $agency_epsn,$agency_user_ID;
 		$this->trip_full_name=$trip_full_name;
 		list($this->agency_name,$this->route_name,$this->set_off_time)=explode(".",$trip_full_name);
 		$this->route_full_name=$this->agency_name.".".$this->route_name;
 		$this->route_belong=new route($this->route_full_name);
 		$this->agency_belong=new dps_user($agency_epsn,$agency_user_ID);
 	}
 	
 	function get_users_belong()
 	{
 		$users_array=$this->agency_belong->get_var_from_ccr(20,$this->trip_full_name);	
 		if($users_array!=false)
 		{
	 		foreach($users_array as $user_array)
	 		{
	 			list($epsn,$user_ID)=explode(".",$user_array["memo"]);
	 			$this->users_belong[]=new traveler($epsn,$user_ID,$this->trip_full_name);
	 		}
	 		$this->user_number=count($this->users_belong);
 		}
 		else
 		{
 			$this->user_number=0;
 		}
 	}
 	
 	function list_users_belong()
 	{
 		global $user_page_processor;
		global $color1,$color2;
		$color_switch=false;
 		if($this->user_number==0)
		{
			echo("目前".$this->set_off_time."出发的".$this->route_name."团尚无人付款报名<br />");
		}
		else
		{
			echo('<form method="POST" action="'.$user_page_processor.'">');
			echo('<table width="200" border="0" align="center">');
			echo('<caption>'.$this->set_off_time."出发的".$this->route_name.'团的报名情况</caption>');
			echo('<tr>');
			echo('<th scope="col">姓名</th>');
			echo('<th scope="col">性别</th>');
			if(func_num_args()==1)
			{
				if(func_get_arg(0)==2)
				{
					echo('<th scope="col">手机号码</th>');
					echo('<th scope="col">接客地址</th>');
					$show_deepth=2;
				}
			}
			echo('<th scope="col">选择</th>');
			echo('</tr>');
			foreach($this->users_belong as $user)
			{
				if($color_switch)
				{
					$color=$color1;
					$color_switch=false;
				}
				else
				{
					$color=$color2;
					$color_switch=true;
				}
				echo('<tr>');
				echo('<td bgcolor='.$color.'>'.$user->realname.'</td>');
				echo('<td bgcolor='.$color.'>'.$user->gender.'</td>');
				if($show_deepth==2)
				{
					echo('<td bgcolor='.$color.'>'.$user->mobile.'</td>');
					echo('<td bgcolor='.$color.'>'.$user->address.'</td>');
				}
				echo('<td bgcolor='.$color.'><input type="radio" name="trip_full_name" value="'.$user->epsn.".".$user->user_ID.'"></td>');
				echo('</tr>');
			}
			echo('</table>');
			echo('<br /><input type="submit" name="submit" value="查看用户详情" disabled>*目前还不能查看团友的信息<br />');
	 		echo('</form>');
		}
 	}
 }
?>
